package com.qingke.pms.command.user;

import com.qingke.pms.command.UserCommand;
import com.qingke.pms.pojo.ProjectLeader;
import com.qingke.pms.pojo.User;
import com.qingke.pms.util.PMSConsole;

public class AddProjectLeaderCommand extends UserCommand {

	public AddProjectLeaderCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		if(!dao.isAdminstrator(user)){
			PMSConsole.println("you are not a adminstrator.");
			return ;
		}
		
		int projectId=PMSConsole.getInt("project id:");
		if(!dao.isProjectExistById(projectId)){
			PMSConsole.println("the project is not existed.");
			return ;
		}
		
		int leaderId=PMSConsole.getInt("leader id:");
		if(!dao.isUserExistById(leaderId)){
			PMSConsole.println("the user is not existed.");
			return ;
		}
		
		ProjectLeader pl=new ProjectLeader();
		pl.setProjectId(projectId);
		pl.setUserId(leaderId);
		
		boolean isExisted=dao.isProjectLeaderExistByProjectAndLeader(pl);
		if(isExisted){
			PMSConsole.println("the user has been the leader of the project");
			return ;
		}
		
		boolean isSuccess=dao.insertProjectLeader(pl);
		if(isSuccess)
			PMSConsole.println("add project leader success.");
		else
			PMSConsole.println("add project leader failed.");
	}

}
